The ImageCombo control is new to Visual Basic and is one of the few controls introduced in Visual Basic 6. In a nutshell, the ImageCombo control is a combo box that supports images and a different indentation for each individual item. This is the same control that Windows uses internally for its file common dialog boxes.
From the programmer's point of view, the main difference between the ImageCombo control and the standard ComboBox control is that the ImageCombo control uses an object-oriented architecture and exposes the ComboItems collection, which in turn contains ComboItem objects.
An ImageCombo control is so similar to a standard ComboBox control that it makes sense to describe only the few differences between them. At design time, you need to set only two properties. The ImageList property is a reference to the ImageList control that contains the images to be displayed beside each ComboItem object. The Indentation property sets the default indentation for all ComboItem objects, expressed as a number of indentation units, where each unit is 10 pixels. Individual ComboItem objects can provide a different value for this property, thus overwriting the default value set in the Properties window at design time or through code at run time.
Like, ComboBox controls, ImageCombo controls can be bound to a data source and therefore support all the usual Dataxxxx properties.
The ImageCombo control exposes many of the properties supported by the regular ComboBox control, including ForeColor, BackColor, Text, SelText, SelStart, SelLength, and Locked. The ImageCombo control doesn't expose any events other than the ones supported by ComboBox.
You see the difference between an ImageCombo control and a ComboBox control when it's time to add items to the control. The ImageCombo control doesn't support the AddItem method. Instead, you add items using the Add method of the ComboItems collection, which has the following syntax:
Add([Index],[Key],[Text],[Image],[SelImage],[Indentation]) As ComboItem |
Index determines where the new ComboItem is inserted, Key is its key in the collection, Text is the string that appears in the control, Image is the associated image (an index or a key in the companion ImageList control), SelImage is the image displayed when the item is selected, and Indentation is the indentation level. (Each unit is 10 pixels.) This syntax allows you to add a new ComboItem and set all its properties in one operation. Here's a routine that loads all the drive letters and volume labels in an ImageCombo control, as you can see in Figure 10-26:
Sub LoadDrivesIntoImageCombo(ImgCombo As ImageCombo) Dim fso As New Scripting.FileSystemObject, dr As Scripting.Drive Dim drLabel As String, drImage As String ' Assume that the ImageCombo control is linked to an ImageList ' control that includes three icons with the following key names. ImgCombo.ComboItems.Add , , "My Computer", "MyComputer" For Each dr In fso.Drives ' Use a different image for each type of drive. Select Case dr.DriveType Case Removable: drImage = "FloppyDrive" Case CDRom: drImage = "CDDrive" Case Else: drImage = "HardDrive" End Select ' Retrieve the letter and (if possible) the volume label. drLabel = dr.DriveLetter & ": " If dr.IsReady Then If Len(dr.VolumeName) Then drLabel = drLabel & "[" & _ dr.VolumeName & "]" End If ' Add an indented item to the combo. ImgCombo.ComboItems.Add , dr.DriveLetter, drLabel, drImage, , 2 Next ' Select the current drive. Set ImgCombo.SelectedItem = ImgCombo.ComboItems(Left$(CurDir$, 1)) End Sub |
Figure 10-26. The ImageCombo demonstration program shows information about all the drives in the system.
You can choose from two ways to select a ComboItem object through code: You can use the SelectedItem property of the ImageCombo control (as shown in the preceding routine), or you can set the Selected property of an individual ComboItem object:
' Select the current drive (alternative method). Set ImgCombo.ComboItems(Left$(CurDir$, 1)).Selected = True |
An interesting effect of dealing with individual ComboItem objects is that you can modify their Text properties without having to remove and add them again, as you would do with a standard ComboBox:
' Change the text of the first item. ImgCombo.ComboItems(1).Text = "My Computer" |
You can delete an individual ComboItem using the Remove method of the ComboItems collection, and you can delete all the items using the collection's Clear method.
The ImageCombo control exposes only one custom method, GetFirstVisible, which returns a reference to the first ComboItem object in the list portion of the control. There isn't much that you can do with this method, however, because you have no way to set the first visible item and therefore you can't programmatically scroll the contents of the list area.
This concludes the description of all the controls embedded in the file MsComCtl.ocx. In the next chapter, I'll describe all the other Windows common controls provided with Visual Basic 6.